package main

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
	"log"
	"strconv"
	"time"
)

/*
* @brief 雨水记录器
* @show 按照ISO时间绑定一个整形，用于显示当前雨量，展示的时候按照一周进行展示，记录是按照每天的日期记录
*/
func main() {
	// 创建一个APP
	waterApp := app.NewWithID("water tracker")
	w := waterApp.NewWindow("Water Tracker")

	// 创建应用配置
	pref := waterApp.Preferences()
	// 设置窗口填充
	w.SetContent(makeWaterUI(pref))
	w.ShowAndRun()
}

// 将时间转换为字符串
func dateKey(time time.Time) string {
	return time.Format("2006-01-02")
}

func makeWaterUI(p fyne.Preferences) fyne.CanvasObject  {
	// 设置总雨量, 并将时间预期绑定，将事件绑定到对应的日期上
	total := binding.BindPreferenceInt(dateKey(time.Now()), p)

	// 创建一个文本用于显示初始值  PrimaryColor returns the color used to highlight primary features
	label := canvas.NewText("0ml", theme.PrimaryColor())
	// 设置文本的大小
	label.TextSize = 39
	// 居中对其
	label.Alignment = fyne.TextAlignCenter
	label.TextStyle.Italic = true

	// 绑定一个Int位string
	totalStr := binding.IntToStringWithFormat(total, "%dml")
	// 添加一个listener每次数据改动的时候都会调用，因此每次total变动，都会出发totalStr变动
	// totalStr变动会触发label被Refresh
	totalStr.AddListener(binding.NewDataListener(
		func() {
			label.Text, _ = totalStr.Get()
			label.Refresh()
		}))

	date := widget.NewLabel(time.Now().Format("Mon Jan 2 2006"))
	date.Alignment = fyne.TextAlignCenter

	amount := widget.NewEntry()
	// 默认250
	amount.SetText("250")

	input := container.NewBorder(nil, nil, nil, widget.NewLabel("ml"), amount)
	add := widget.NewButton("Add", func() {
		inc, err := strconv.Atoi(amount.Text)
		if err != nil {
			log.Println("Failed to parse integer: " + amount.Text)
			return
		}
		 current, _ := total.Get()
		_ = total.Set(current + inc)
	})

	// 每周初
	weekStart := dateForMonday()
	dayLength :=  time.Hour * 24

	history := container.NewGridWithColumns(2,
		widget.NewLabel("Monday"), historyLabel(weekStart, p),
		widget.NewLabel("Tuesday"), historyLabel(weekStart.Add(dayLength), p),
		widget.NewLabel("Wednesday"), historyLabel(weekStart.Add(dayLength * 2), p),
		widget.NewLabel("Thursday"), historyLabel(weekStart.Add(dayLength * 3), p),
		widget.NewLabel("Friday"), historyLabel(weekStart.Add(dayLength * 4), p),
		widget.NewLabel("Saturday"), historyLabel(weekStart.Add(dayLength * 5), p),
		widget.NewLabel("Sunday"), historyLabel(weekStart.Add(dayLength * 6), p))


	return container.NewVBox(label, date,
		container.NewGridWithColumns(2, input, add),
		widget.NewCard("History", "Totals this week", history))
}

func dateForMonday() time.Time {
	day :=  time.Now().Weekday()
	/*
	const (
		Sunday Weekday = iota
		Monday
		Tuesday
		Wednesday
		Thursday
		Friday
		Saturday
	)
	*/
	// 因为周日比较特殊，所以这里周日的时候其实是减去6天算的是上周一的时间
	if day == time.Sunday {
		return time.Now().Add(-1 * time.Hour * 24 * 6)  // monday of previous week
	}

	// 如果不是周一，就计算出周一至今的时间
	daysSinceMonday := time.Duration(day - 1)
	// 计算一天的时长
	dayLength := time.Hour * 24
	// Add returns the time t+d.
	// 当前时间减去周一至今的时长就是周一的时间
	return time.Now().Add(-1 * dayLength * daysSinceMonday)
}

func historyLabel(date time.Time, p fyne.Preferences) fyne.CanvasObject {
	data := binding.BindPreferenceInt(dateKey(date), p)

	str := binding.IntToStringWithFormat(data, "%dml")
	num := widget.NewLabelWithData(str)
	num.Alignment = fyne.TextAlignCenter
	return num
}

